const { ccclass, property } = cc._decorator;

@ccclass
export default class killsControl extends cc.Component {
    @property(cc.Label)
    labelK: cc.Label = null;
    @property(cc.Label)
    labelD: cc.Label = null;
    @property(cc.Label)
    reLife: cc.Label = null;

    @property(cc.Label)
    armorA: cc.Label = null;
    @property(cc.Label)
    armorC: cc.Label = null;
    @property(cc.Label)
    Attack: cc.Label = null;
    @property(cc.Label)
    speed: cc.Label = null;
    @property(cc.Label)
    jump: cc.Label = null;


    @property(cc.Label)
    lifeValue: cc.Label = null;
    @property(cc.ProgressBar)
    bar: cc.ProgressBar = null;

    @property({
        type: [cc.Component.EventHandler],
        tooltip: "游戏结束"
    })
    kdaOverEvents: Array<cc.Component.EventHandler> = [];

    _heroManger: HeroManager = null;

    onLoad() {
        this.labelK.string = "--";
        this.labelD.string = "--";
    }

    start() {
        this._heroManger = HeroManager.getInstance();
        this.reLife.string = "+" + this._heroManger.lifeRegain + "/S";
        this.schedule(this.updateKD, 0.4);
    }
    updateKD() {
        this.setKills();
        this.setBruise();
        this.setLife();
        this.armorA.string = this._heroManger.AttributeImmunity.toFixed(0);
        this.armorC.string = this._heroManger.CommonImmunity.toFixed(0);
        this.Attack.string = Math.abs(this._heroManger.damageAttack).toFixed(0);
        this.speed.string = this._heroManger.runSpeed.toFixed(0);
        this.jump.string = this._heroManger.jumpSpeed.toFixed(0);
    }

    setKills() {
        if (this._heroManger.passkillCounts > 0) {
            if (this._heroManger.killCounts >= this._heroManger.passkillCounts)
                this.gameOver(true);
            this.labelK.string = this._heroManger.killCounts.toString() + "/" + this._heroManger.passkillCounts;
        }
        else {
            this.labelK.string = this._heroManger.killCounts.toString();
        }
    }
    setBruise() {
        if (this._heroManger.passHitCounts > 0) {
            if (this._heroManger.HitCounts >= this._heroManger.passHitCounts)
                this.gameOver(false);
            this.labelD.string = this._heroManger.HitCounts.toString() + "/" + this._heroManger.passHitCounts.toString();
        }
        else {
            this.labelD.string = this._heroManger.HitCounts.toString();
        }
    }
    setLife() {
        this.bar.progress = this._heroManger.life / this._heroManger.lifeMax;
        this.lifeValue.string = this._heroManger.life.toFixed(0) + " / " + this._heroManger.lifeMax.toString();
    }
    gameOver(iswin: boolean) {
        cc.Component.EventHandler.emitEvents(this.kdaOverEvents, {
            target: this,
            iswin: iswin
        });
    }
}
